package somar;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.XMLReader;

import somar_test_hello.parser.rselV1_4a.ListVisitor;
import somar_test_hello.parser.rselV1_4a.RSELSaxEvent;


public class RSELRegistrationUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JButton registRSELButton = new JButton();
	
	public RSELRegistrationUI() {
		
		registRSELButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fChooser = new JFileChooser();
				FileFilter fFilter = new FileNameExtensionFilter("RSEL files", "rsel");
				fChooser.addChoosableFileFilter(fFilter);
				
				fChooser.showOpenDialog(null);
				if(fChooser.getSelectedFile() == null) {
					System.out.println("[SOAMR Server] - RSEL Registration Module : RSEL file is not selected.");
				} else {
					System.out.println(fChooser.getSelectedFile().getPath());
					System.out.println(fChooser.getSelectedFile().getName());
				}
				// validation check of rsel
				
			
			}
			
		});
		
		initialize();
		
	}

	public void initialize() {

		// set look and feel		
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		
		
		this.setTitle("SOMAR RSELRegistration Module");
		this.setBounds(100, 100, 500, 500);
		this.setVisible(true);
	
		
		Container cpane = this.getContentPane();
		this.registRSELButton.setBounds(10, 10, 10, 10);
		cpane.setLayout(null);
		cpane.add(registRSELButton);
		
	}
	
	
	public void checkRSEL(String rselPath) {
		
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			XMLReader reader = parser.getXMLReader();
			
			RSELSaxEvent se = new RSELSaxEvent();
			reader.setContentHandler(se);
			reader.parse(rselPath);
			
			se.getRootElement().accept(new ListVisitor());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
